package com.szqhyxj.youxiangju.sys

import com.szqhyxj.youxiangju.BuildConfig
import com.szqhyxj.youxiangju.biz.core.TokenManager
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitFacade {

    companion object {

        private val gsonConverterFactory = GsonConverterFactory.create()

        private val rxJava2CallAdapterFactory = RxJava2CallAdapterFactory.create()

        val retrofit: Retrofit
        val uploadRetrofit: Retrofit
        val mockretrofit: Retrofit

        init {
            val apiClient = OkHttpClient.Builder()
                    .addInterceptor(HeaderInterceptor())
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(HttpLoggingInterceptor().apply {
                        this.level = HttpLoggingInterceptor.Level.BODY
                    }).build()
            val imageClient = OkHttpClient.Builder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .addInterceptor(HeaderInterceptor())
                    .addInterceptor(
                            HttpLoggingInterceptor()
                                    .apply {
                                        this.level = HttpLoggingInterceptor.Level.HEADERS
                                    })
                    .build()
            retrofit = Retrofit.Builder()
                    .baseUrl(BuildConfig.API)
                    .client(apiClient)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJava2CallAdapterFactory)
                    .build()
            uploadRetrofit = Retrofit.Builder()
                    .baseUrl(BuildConfig.IMAGE_API)
                    .client(imageClient)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJava2CallAdapterFactory)
                    .build()
            mockretrofit = Retrofit.Builder()
                    .baseUrl(BuildConfig.MOCK_API)
                    .client(apiClient)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJava2CallAdapterFactory)
                    .build()
        }
    }

    class HeaderInterceptor : Interceptor {
        override fun intercept(chain: Interceptor.Chain): Response {
            var request = chain.request()
            if (TokenManager.mToken.isNotEmpty()) {
                request = request.newBuilder().addHeader("token", TokenManager.mToken).build()
            } else {
                request = request.newBuilder().build()
            }
            return chain.proceed(request)
        }
    }
}